package com.dxg.msns.music.controller;

import com.dxg.msns.common.util.Result;
import com.dxg.msns.music.pojo.Music;
import com.dxg.msns.music.pojo.MusicVo;
import com.dxg.msns.music.pojo.Playlist;
import com.dxg.msns.music.service.PlaylistGradeService;
import com.dxg.msns.music.service.PlaylistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/music/playlists")
public class PlaylistController {
    @Autowired
    private PlaylistGradeService playlistGradeService;

    @Autowired
    private PlaylistService playlistService;
    @PostMapping("/createPlaylist")
    public ResponseEntity<?> create(@RequestBody Playlist playlist) {
        playlistService.createPlaylist(playlist);
        return ResponseEntity.ok("创建成功");
    }
    @GetMapping("/recommend")
    public ResponseEntity<?> recommendPlaylist() {
        System.out.println("recommendPlaylist");
        List<Playlist> playlists = playlistService.recommendPlaylist();
        return ResponseEntity.ok(playlists);
    }
    @GetMapping("/{id}")
    public Playlist getById(@PathVariable String id) {
        return playlistService.getPlaylistById(id);
    }
    @GetMapping("/userid/{userId}")
    public List<Playlist> getByUserId(@PathVariable String userId) {
        return playlistService.getPlaylistByUserId(userId);
    }
    @GetMapping("/grade")
    public ResponseEntity<String> gradePlaylist(@RequestParam String playlistId,
                                                @RequestParam String userId,
                                                @RequestParam Integer grade) {
        System.out.println("playlistId");
        if (grade < 1 || grade > 100) {
            return ResponseEntity.badRequest().body("评分必须在1到100之间。");
        }
        String result = playlistGradeService.ratePlaylist(playlistId, userId, grade);
        return ResponseEntity.ok(result);
    }
    @PutMapping
    public ResponseEntity<?> update(@RequestBody Playlist playlist) {
        playlistService.updatePlaylist(playlist);
        return ResponseEntity.ok("更新成功");
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable String id) {
        playlistService.deletePlaylistById(id);
        return ResponseEntity.ok("删除成功");
    }

    @PostMapping("/{id}/songs/{musicId}")
    public ResponseEntity<?> addMusic(@PathVariable String id, @PathVariable String musicId) {
        playlistService.addMusicToPlaylist(id, musicId);
        return ResponseEntity.ok("添加歌曲成功");
    }

    @DeleteMapping("/{id}/songs/{musicId}")
    public ResponseEntity<?> removeMusic(@PathVariable String id, @PathVariable String musicId) {
        playlistService.removeMusicFromPlaylist(id, musicId);
        return ResponseEntity.ok("移除歌曲成功");
    }
    @GetMapping("/getAll")
    public Result<?> getAll() {
        return Result.success(playlistService.getAllPlaylists());
    }

    @GetMapping("/getMusicByPlaylistId")
    public Result<List<MusicVo>> getMusicIdsByPlaylistId(@RequestParam String playlistId,String userId) {
        return Result.success(playlistService.getMusicByPlaylistId(playlistId,userId));
    }

    @GetMapping("/getMusicByPlaylistIdAdmin")
    public Result<List<Music>> getMusicByPlaylistIdAdmin(@RequestParam String playlistId) {
        return Result.success(playlistService.getMusicByPlaylistIdAdmin(playlistId));
    }
}
